/*
 * ChatFrame.java
 *
 * Created on 14 novembre 2007, 21:29
 */

package jtalk.view;

import jtalk.controller.DefaultController;
import jtalk.model.Contact;
import jtalk.model.Message;

/**
 *
 * @author  remy
 */
public class ChatFrame extends javax.swing.JFrame {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -7266858009982252487L;
	
	/** Creates new form ChatFrame */
    public ChatFrame(Contact participant, DefaultController controller) {
    	this.participant = participant;
    	this.controller = controller;
        initComponents();        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        chatScrollPane = new javax.swing.JScrollPane();
        chatTextArea = new javax.swing.JTextArea();
        sendButton = new javax.swing.JButton();
        sendScrollPane = new javax.swing.JScrollPane();
        sendTextArea = new javax.swing.JTextArea();

        setTitle(this.participant.getNickName());
        chatTextArea.setColumns(20);
        chatTextArea.setEditable(false);
        chatTextArea.setLineWrap(true);
        chatTextArea.setRows(5);
        chatTextArea.setWrapStyleWord(true);
        chatScrollPane.setViewportView(chatTextArea);

        sendButton.setText("send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        sendScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        sendTextArea.setColumns(20);
        sendTextArea.setLineWrap(true);
        sendTextArea.setRows(5);
        sendTextArea.setWrapStyleWord(true);
        sendTextArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        sendTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sendTextAreaKeyTyped(evt);
            }
        });

        sendScrollPane.setViewportView(sendTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chatScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sendScrollPane)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chatScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sendButton)
                        .addGap(22, 22, 22))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sendScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendTextAreaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sendTextAreaKeyTyped
        if(evt.getKeyChar() == '\n') {                
        	sendMessage();
        }
    }//GEN-LAST:event_sendTextAreaKeyTyped

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
    	sendTextArea.append("\n");
        sendMessage();
    }//GEN-LAST:event_sendButtonActionPerformed

	private void sendMessage() {
		if(!sendTextArea.getText().matches("^[\n\t ]*$")) {		
			Message msg = new Message();
	    	msg.setTo(this.participant);
	    	msg.setMessage(sendTextArea.getText());
	    	controller.sendMessage(msg);
	    	sendTextArea.setText("");    	
	    	chatTextArea.append("me : ");
	    	chatTextArea.append(msg.getMessage());
	    	insertNewLine();
	    	chatTextArea.setCaretPosition(chatTextArea.getDocument().getLength ());			
		}else {
			sendTextArea.setText("");
		}
	}
    
    protected void receivedMessage(Message msg) {    	
    	this.chatTextArea.append(msg.getFrom().getNickName() + " : ");
    	this.chatTextArea.append(msg.getMessage());
    	insertNewLine();
    	chatTextArea.setCaretPosition(chatTextArea.getDocument().getLength ());
    }
    
    private void insertNewLine() {
    	if(!chatTextArea.getText().endsWith("\n")) {
    		chatTextArea.append("\n");
    	}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane chatScrollPane;
    private javax.swing.JTextArea chatTextArea;
    private javax.swing.JButton sendButton;
    private javax.swing.JScrollPane sendScrollPane;
    private javax.swing.JTextArea sendTextArea;
    // End of variables declaration//GEN-END:variables
    
    //Other declarations
    private Contact participant;
    private DefaultController controller;
    //End
    
	public final Contact getParticipant() {
		return participant;
	}
}
